﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace CoffeePotServer
{
    /// <summary>
    /// File: Client_Response.cs
    /// Author: James Dibble 10009689
    /// Last Revision: 17/03/12
    /// SVN Address:
    /// http://code.google.com/p/cnos-coffeepot-server/source/browse/branches/DibbleBranch/CoffeePotServerLib/CoffeePotServerLib/Client_Response.cs
    /// </summary>
    public class Client_Response
    {
        #region Attributes
        private Socket _client;
        private NetworkStream _networkStream;
        private const int BufferSize = 1024;
        #endregion

        #region Constructor
        /// <summary>
        /// Create an object to listen for messages on a network stream.
        /// </summary>
        /// <param name="networkStream"></param>
        /// <param name="client"></param>
        public Client_Response(NetworkStream networkStream, Socket client)
        {
            _networkStream = networkStream;
            _client = client;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Get the next message from the network stream.
        /// </summary>
        /// <returns>The message.</returns>
        public String ReadLine()
        {
            if (_networkStream.CanRead)
            {
                String strMessage = "";
                byte[] byteBuffer = new byte[1024];
                int intNumBytes = _networkStream.Read(byteBuffer, 0, byteBuffer.Length);
                strMessage += ByteArrayToString(byteBuffer, intNumBytes);
                return strMessage;
            }
            return null;
        }

        //From my generic library.
        /// <summary>
        /// Convert a byte array into a string.
        /// </summary>
        /// <param name="bytArrToConvert">The byte array to be converted.</param>
        /// <param name="intLength">The length of the anticipated string.</param>
        /// <returns>The string that was contained in the byte array.</returns>
        private String ByteArrayToString(byte[] bytArrToConvert, int intLength)
        {
            return Encoding.ASCII.GetString(bytArrToConvert, 0, intLength);
        }
        #endregion
    }
}